/*
* Copyright (C) 2021 ~ 2022 Uniontech Technology Co., Ltd.
*
* Author:      zhangbingyi <zhangbingyi@uniontech.com>
*
* Maintainer:  zhangbingyi <zhangbingyi@uniontech.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#pragma once

#include "widgets/defendertable.h"
#include "usbwhitelistdelegate.h"
#include "common/usbmanagercomm.h"
#include "usbstoragesecuritymodel.h"
#include "usbdevicedetailsdlg.h"
#include "widgets/ddialog.h"

#include <QCommandLinkButton>
#include <QLabel>
#include <QPushButton>
#include <QWidget>

using namespace def::widgets;
class USBWhiteListDlg : public QFrame
{
    Q_OBJECT

public:
    explicit USBWhiteListDlg(USBStorageSecurityModel *model, QWidget *parent = nullptr);
    virtual ~USBWhiteListDlg() override;

private:
    // 初始化界面
    void initUI();
    // 初始化信号连接
    void initConnection();
    // 初始化中间tableview布局
    QHBoxLayout *initTableWidget();
    // 向白名单表中添加数据
    void appendWhitelist(const USBConnectionInfo &info);
    // 插入一条白名单信息
    void insertWhitelist(int row, const USBConnectionInfo &info);
    // 移除白名单 界面改变
    void removeWhitelist(const USBConnectionInfo &info);
    // 修改白名单 界面改变
    void modifyWhitelist(const USBConnectionInfo &info);

    // 通过USB连接信息获得数据项
    QStandardItem *createDataItemFromInfo(const USBConnectionInfo &info);
    // 执行策略 表格加载下拉框
    void createEditor();
    Q_SLOT void createEditors(const QModelIndex &parent, int first, int last);

    // 通过设备信息获取modelIndex
    bool getModelIndex(QModelIndex &index, const USBConnectionInfo &info);
    // 通过设备信息获取modelIndex
    USBConnectionInfo getInfoFromIndex(QModelIndex &index);

    // 界面是否启用状态刷新
    Q_SLOT void updateUI();

Q_SIGNALS:
    // 错误信息信号
    void sendError(int type, const QString &str);

private Q_SLOTS:
    // 显示 USB详情页弹窗
    void showDetailsDlg(USBConnectionInfo &info);
    // 接收USB设备白名单
    void onRecvAllWhitelist(const USBConnectionInfoList &list);
    // delegate 响应移除白名单操作
    void onNotifyOperateWhitelist(int type, const USBConnectionInfo &info);
    // 当白名单数据操作完成
    void onOperateStorageWhitelistFinished(int opType, const USBConnectionInfo &info);
    // 当批量导入白名单完成
    void onImportBatchStorageWhitelistFinished(const USBConnectionInfoList &list);
    // 批量添加白名单 界面改变
    void addBatchWhitelist(const USBConnectionInfoList &list);
    // 批量修改白名单 界面改变
    void modifyBatchWhitelist(const USBConnectionInfoList &list);
    // 新连接USB的Name改变，刷新白名单Name显示
    void updateNameAndSizeInWhitelist(const USBConnectionInfo &info, const QString &name, qint64 size);

    void onNotifyOperateWhitelistHelp(int index, const QString &);
protected:
    void closeEvent(QCloseEvent *event) override;

private:
    USBStorageSecurityModel *m_usbStorageSecurityModel; // 数据类
//    DBlurEffectWidget *m_blurEffect; // 透明背景
    DefenderTableHeaderView *m_headerDelegate; // 表头代理类
    QTableView *m_whitelistTable; // 表格
    QStandardItemModel *m_standardItemModel; // QT表格模型

    QCommandLinkButton *m_downloadBtn; // 下载模板按钮
    QCommandLinkButton *m_importBtn; // 导入记录按钮

    USBWhitelistDelegate *m_USBWhitelistDelegate; // 白名单表格代理
    USBDeviceDetailsDlg *m_USBDeviceDetails; // USB详情页弹窗
};
